package com.ffapp.helper.fragment.passageway;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.ffapp.helper.R;
import com.ffapp.helper.basis.BasisFragment;
import com.ffapp.helper.bean.ParamListData;
import com.ffapp.helper.util.PassWayUtil;
import com.ffapp.helper.util.SoftKeyBoardListener;
import com.suke.widget.SwitchButton;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class DIAlarmFragment extends BasisFragment {

    private String hardwareInterface;
    private List<ParamListData> passWayList;

    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;
    MyAdapter adapter;
    ArrayList<ParamListData> showList = new ArrayList<>();

    ParamListData sAlarmDo;  //报警联动关联DO

    public static DIAlarmFragment newInstance() {
        DIAlarmFragment fragment = new DIAlarmFragment();
        return fragment;
    }

    public static DIAlarmFragment newInstance(String hardwareInterface, List<ParamListData> passWayList) {
        DIAlarmFragment fragment = new DIAlarmFragment();
        fragment.hardwareInterface = hardwareInterface;
        fragment.passWayList = passWayList;
        return fragment;
    }

    @Override
    public void initViews() {
        super.initViews();
        setContentView(R.layout.fragment_dialarm);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new MyAdapter();
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        readData(hardwareInterface,passWayList);
    }


    //回读
    public void readData(String hardwareInterface, List<ParamListData> passWayList) {
        if (!TextUtils.isEmpty(hardwareInterface) && passWayList.size()>1&& isAdded()) {
            showList.clear();
            PassWayUtil.hardwareInterfaceIsChange(hardwareInterface,passWayList);
            switch (hardwareInterface) {
                case "DI0":
                case "DI1":
                case "DI2":
                case "DI3":
                    for (ParamListData data : passWayList) {
                        switch (data.getName()) {
                            case "报警触发方式":
                            case "报警内容":
                            case "报警DO0":
                            case "报警DO1":
                            case "报警DO2":
                                showList.add(data);
                                break;
                            case "阈值DO关联":
                                sAlarmDo = data;
                                break;
                        }
                    }
                    break;
            }
            adapter.notifyDataSetChanged();
            recyclerView.setAdapter(adapter);
        }
    }

    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

        public MyAdapter() {

        }

        //创建新View，被LayoutManager所调用
        @Override
        public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.fragment_pass_way_base_item, viewGroup, false);
            return new MyAdapter.ViewHolder(view);
        }


        //将数据与界面进行绑定的操作
        @Override
        public void onBindViewHolder(MyAdapter.ViewHolder viewHolder, int position) {
            viewHolder.list_item.setTag(position);
            viewHolder.bind(position);
        }

        //获取数据的数量
        @Override
        public int getItemCount() {
            return showList == null ? 0 : showList.size();
        }


        //自定义的ViewHolder，持有每个Item的的所有界面元素
        public class ViewHolder extends RecyclerView.ViewHolder {

            @BindView(R.id.list_item)
            LinearLayout list_item;
            @BindView(R.id.tv_name)
            TextView tvName;
            @BindView(R.id.edit_text)
            EditText editText;
            @BindView(R.id.img_spanner)
            ImageView imgSpanner;
            @BindView(R.id.tv_unit)
            TextView tvUnit;
            @BindView(R.id.switch_btn)
            SwitchButton switchBtn;
            @BindView(R.id.rg_relay)
            RadioGroup relayRg;

            public ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }

            public void bind(int position) {
                ParamListData terminalListData = showList.get(position);
                tvName.setText(terminalListData.getName());
                switch (terminalListData.getType()) {
                    case 1: //spinner
                        editText.setText("" + (TextUtils.isEmpty(terminalListData.getValue()) ? terminalListData.getKey() : terminalListData.getValue()));
                        editText.setHint("请选择" + terminalListData.getName());
                        editText.setInputType(terminalListData.getinputType_REAL());
                        imgSpanner.setVisibility(View.VISIBLE);
                        tvUnit.setVisibility(View.GONE);
                        switchBtn.setVisibility(View.GONE);
                        relayRg.setVisibility(View.GONE);
                        editText.setEnabled(true);//不可编辑
                        list_item.setBackgroundResource(R.color.white);
                        break;
                    case 2:  // edit
                        editText.setText("" + terminalListData.getValue());
                        editText.setHint("请输入" + terminalListData.getName());
                        editText.setInputType(terminalListData.getinputType_REAL());
                        tvUnit.setVisibility(View.VISIBLE);
                        tvUnit.setText(terminalListData.getUnit());
                        imgSpanner.setVisibility(View.GONE);
                        switchBtn.setVisibility(View.GONE);
                        relayRg.setVisibility(View.GONE);
                        editText.setEnabled(true);//可编辑
                        editText.setVisibility(View.VISIBLE);
                        list_item.setBackgroundResource(R.color.white);
                        break;
                    case 5:  // radioGroup
                        imgSpanner.setVisibility(View.GONE);
                        tvUnit.setVisibility(View.GONE);
                        editText.setVisibility(View.GONE);
                        list_item.setBackgroundResource(R.color.white);
                        switchBtn.setVisibility(View.GONE);
                        relayRg.setVisibility(View.VISIBLE);
                        switch (TextUtils.isEmpty(terminalListData.getValue()) ? terminalListData.getKey() : terminalListData.getValue()) {
                            case "0":
                                relayRg.check(R.id.rg_unrelated);
                                break;
                            default:
                                relayRg.check(R.id.rg_related);
                                break;
                        }
                        relayRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                            @Override
                            public void onCheckedChanged(RadioGroup group, int checkedId) {
                                int value =Integer.valueOf(TextUtils.isEmpty(terminalListData.getValue()) ? terminalListData.getKey() : terminalListData.getValue());
                                switch (checkedId) {
                                    case R.id.rg_unrelated:   //不关联
                                        switch (terminalListData.getName()){
                                            case "报警DO0":
                                                changeDoOfMinus(terminalListData,sAlarmDo,value,1);
                                                break;
                                            case "报警DO1":
                                                changeDoOfMinus(terminalListData,sAlarmDo,value,2);
                                                break;
                                            case "报警DO2":
                                                changeDoOfMinus(terminalListData,sAlarmDo,value,4);
                                                break;
                                        }
                                        break;
                                    case R.id.rg_related:  //关联
                                        switch (terminalListData.getName()){
                                            case "报警DO0":
                                                changeDoOfPlus(terminalListData,sAlarmDo,value,1);
                                                break;
                                            case "报警DO1":
                                                changeDoOfPlus(terminalListData,sAlarmDo,value,2);
                                                break;
                                            case "报警DO2":
                                                changeDoOfPlus(terminalListData,sAlarmDo,value,4);
                                                break;
                                        }
                                        break;
                                }
                            }
                        });
                        break;
                }
                editText.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        int curPosition = getAdapterPosition();
                        ParamListData data = showList.get(curPosition);
                        data.setChange(true);
                        data.setValue(s.toString());
                    }
                });
                imgSpanner.setTag(terminalListData);
            }

            @OnClick(R.id.img_spanner)
            public void showSpanner(View v) {
                SoftKeyBoardListener.hideKeyboard(getActivity());
                final ParamListData terminalListData = (ParamListData) v.getTag();
                OptionsPickerView pickerView = new OptionsPickerBuilder(mContext, new OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        editText.setText(terminalListData.getValues().get(options1));
                    }
                })
                        .setSubmitText("确定")//确定按钮文字
                        .setCancelText("取消")//取消按钮文字
                        .build();
                pickerView.setPicker(terminalListData.getValues());
                pickerView.show();
            }
        }
    }

    private void changeDoOfMinus(ParamListData data,ParamListData dataDo,int value,int minuend){
        value = value-minuend;
        data.setValue(""+ value);
        dataDo.setChange(true);
        dataDo.setValue(""+(Integer.valueOf(dataDo.getValue())-minuend));
    }

    private void changeDoOfPlus(ParamListData data,ParamListData dataDo,int value,int minuend){
        value = value+minuend;
        data.setValue(""+ value);
        dataDo.setChange(true);
        dataDo.setValue(""+(Integer.valueOf(dataDo.getValue())+minuend));
    }
}
